var outilPopupWidthPopup;
var outilPopupHeightPopup;
var outilPopupPopupOuverte = false;
var onResizeUser;
var overflowInitial;
var outilPopupLargeurBordure = 20;
var outilPopupPadding = 10;
var outilPhotoGrandePopup;

function redimmensionnementElements() {
	//redimmensionnement du fond
	$('#masqueApplication').css("width", getOutilPopupWidthEcranVisible());
	$('#masqueApplication').css("height", getOutilPopupHeightEcranVisible());
	
	//redimmensionnement de la popup (si c'est une grande popup)
	if (outilPhotoGrandePopup == true) {
		setOutilPopupWidthPopup(getWidthGrandePopup());
		setOutilPopupHeightPopup(getHeightGrandePopup());
	}
	$('#popup_block').css({
		'width': outilPopupWidthPopup,
		'height': outilPopupHeightPopup 
	});
	positionneElements();
}

function positionneElements() {
	var scrollLeft = getScrollXY()[0];
	var scrollTop = getScrollXY()[1];
	
	//Positionne le fond
	$('#masqueApplication').css("left", scrollLeft);
	$('#masqueApplication').css("top", scrollTop);
	
	//Positionne la fenêtre
	var popMargTop = (getOutilPopupHeightEcranVisible() - outilPopupHeightPopup) / 2;
	var popMargLeft = (getOutilPopupWidthEcranVisible()  - outilPopupWidthPopup) / 2;
	
	var positionTop =  popMargTop + scrollTop - outilPopupLargeurBordure - outilPopupPadding;
	var positionLeft = popMargLeft + scrollLeft - outilPopupLargeurBordure - outilPopupPadding;
	$('#popup_block').css({
		'top' : positionTop,
		'left' : positionLeft
	});
	
	//Positionne le bouton fermer
	$("#boutonFermer").css('top', -16);
	$("#boutonFermer").css('left', outilPopupWidthPopup -16 + outilPopupPadding * 2);
}

function ouvrirGrandePopup(page, onLoad, onResize) {
	onResizeUser = onResize;
	if (onResize != null) {
		//$(window).bind('resize', onResize);
		$(window).resize(onResize);
	}
	ouvrirPopup(page, getWidthGrandePopup(), getHeightGrandePopup(), onLoad, true);
}

function ouvrirPopup(page, popWidth, popHeight, onLoad, grandePopup) {
	if (outilPopupPopupOuverte) {
		return;
	}
	if (grandePopup == true) {
		outilPhotoGrandePopup = true;
	}else {
		outilPhotoGrandePopup = false;
	}
	setOutilPopupWidthPopup(popWidth);
	setOutilPopupHeightPopup(popHeight);
	
	outilPopupPopupOuverte = true;
	
	$(window).bind('resize', redimmensionnementElements);
	$(window).bind('scroll', positionneElements);
	
	
	jQuery('<div/>', {
	    id: 'masqueApplication',
	    onclick : 'fermerPopup();'
	}).appendTo('body');
	
	jQuery('<div/>', {
	    id: 'popup_block'
	}).appendTo('body');
	
	$('#popup_block').fadeIn();
	
	overflowInitial =$('body').css("overflow"); 
	$('body').css("overflow", "hidden"); 
	
	//Apparition du fond - .css({'filter' : 'alpha(opacity=80)'}) pour corriger les bogues de IE
	$('#masqueApplication').css({'filter' : 'alpha(opacity=80)'}).fadeIn();
	
	$.post(page, function(data) {
		$('#popup_block').css('position','absolute').css('left','0').css('right','0').css('top','0').css('bottom','0');
		$('#popup_block').html(data); 
		jQuery('<img/>', {
		    id: 'boutonFermer',
		    src:'/icones/supprimer-32.png', 
		    onMouseout:"this.src='/icones/supprimer-32.png';", 
		    onMouseOver:"this.src='/icones/supprimer-32-hover.png';",
		    onclick : 'fermerPopup();'
		}).appendTo('#popup_block');
		
		if (onLoad != null) {
			onLoad();
		}
		redimmensionnementElements();
	});
}

function fermerPopup() {
	//suppression des évènements
	$(window).unbind('resize', redimmensionnementElements);
	$(window).unbind('scroll', positionneElements);
	if (onResizeUser != null) {
		$(window).bind('resize', onResizeUser);
	}
	
	//Suppression des composants de la popup
	$('#masqueApplication').fadeOut(300, function() { $(this).remove(); });
	$("#boutonFermer").fadeOut(300, function() { $(this).remove(); });
	$('#popup_block').fadeOut(300, function() { $(this).remove(); });
	
	//restauration de l'overflow
	$('body').css("overflow", overflowInitial); 
	outilPopupPopupOuverte = false;
	return false;
}

/////////////////Méthodes utilitaires////////////////////
function getOutilPopupHeightEcranVisible() {
	return $(window).innerHeight();
}

function getOutilPopupWidthEcranVisible() {
	return $(window).innerWidth();
}

function setOutilPopupWidthPopup(popWidth) {
	//if (navigator.appName == 'Microsoft Internet Explorer') {
	//	outilPopupWidthPopup = popWidth + 2*outilPopupLargeurBordure + outilPopupPadding;
	//} else {
		outilPopupWidthPopup = popWidth;
	//}
} 

function setOutilPopupHeightPopup(popHeight) {
	//if (navigator.appName == 'Microsoft Internet Explorer') {
	//	outilPopupHeightPopup = popHeight + 2*outilPopupLargeurBordure + outilPopupPadding;
	//} else {
		outilPopupHeightPopup = popHeight;
	//}
} 

function getWidthGrandePopup() {
	return getOutilPopupWidthEcranVisible() - 200;
}
function getHeightGrandePopup() {
	return getOutilPopupHeightEcranVisible() - 150;
}
function getScrollXY() {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return [ scrOfX, scrOfY ];
	}